using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FiltronASP.Helpers.ActionFilters
{
    /// <summary>
    /// Klasa odpowiedzialna za walidacje tylko atrybutow widocznych na formularzu.
    /// </summary>
    public class ValidateOnlyIncomingValuesAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Metoda zmieniajaca domyslne zachowanie walidacji.
        /// </summary>
        /// <param name="filterContext">kontekst</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var modelState = filterContext.Controller.ViewData.ModelState;
            var valueProvider = filterContext.Controller.ValueProvider;
            var keysWithNoIncomingValue = modelState.Keys.Where(x => !valueProvider.ContainsPrefix(x));
            foreach (var key in keysWithNoIncomingValue) {
                modelState[key].Errors.Clear();
            }   //foreach
        }

    }
}
